package com.neusoft.task;
import java.net.URL;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.neusoft.common.BaseActionSupport;
import com.neusoft.common.Const;
import com.neusoft.common.util.Pagenation;
import com.neusoft.init.InitTaskService;
import com.neusoft.util.Configuration;

public class TaskAction extends BaseActionSupport {

	private static final long	serialVersionUID	= 213559172667851867L;
	
	private Pagenation page;
	private TaskBean queryBean;
	private TaskBean updateBean;
	private TaskBean addBean;
	private static URL appPath = Thread.currentThread().getContextClassLoader().getResource("");
	private static String propertyFile = "videoSuperintendConfig.properties";

	/*
	 * 登录页面
	 */
	public String login(){
		return "login";
	}
	/**
	 * 进入系统
	 * @author 郑兴龙  
	 */
	public String loginIn(){
		HttpServletRequest req=ServletActionContext.getRequest();
		String name=req.getParameter("userid");
		String password=req.getParameter("password");
		TaskBean _task=getTaskInfo();
		
		//没回显功能		
		if(null!=name&&name.length()>0&&null!=password&&password.length()>0){
			if(_task.getUserid().equals(name.trim().toLowerCase())&&_task.getPassword().equals(password.trim())){
				req.getSession().setAttribute(Const.SESSION_TASK_INFO, _task);
				queryBean=_task;
				return "index";
			}
			
		}
		Const.printScript(ServletActionContext.getResponse(),"<script>alert('您输入的用户名或密码错误，请从新输入');window.parent.location.href='"+ServletActionContext.getRequest().getContextPath()+"/task/task_login.action';</script>");
		return null;
	}
	
	private TaskBean getTaskInfo(){
		TaskBean taskBean = new TaskBean();
		Map<String,String>_temp = Configuration.readProperties(appPath.getPath()+propertyFile);
		taskBean.setTaskid(_temp.get("taskId"));
		taskBean.setTimer(_temp.get("timer"));
		taskBean.setUserid(_temp.get("USER"));
		taskBean.setPassword(_temp.get("PASSWORD"));
		taskBean.setIp(_temp.get("IP"));
		taskBean.setLogin_port(_temp.get("LOGIN_PORT"));
		taskBean.setSru_port(_temp.get("SRU_PORT"));
		taskBean.setScu_smu_port(_temp.get("SCU_SMU_PORT"));
		taskBean.setLocalhostIP(_temp.get("LocalhostIP"));
		taskBean.setLocalhostPort(_temp.get("LocalhostPort"));
		return taskBean;
	}
	public String index(){
		TaskBean _task=getTaskInfo();
		queryBean=_task;
		return "index";
	}
	
	public String modSave(){
		String rsString="操作失败，请稍后再试";	
		
		Configuration.writeProperties(appPath.getPath()+propertyFile, "USER", updateBean.getUserid());
		Configuration.writeProperties(appPath.getPath()+propertyFile, "PASSWORD", updateBean.getPassword());
		Configuration.writeProperties(appPath.getPath()+propertyFile, "IP", updateBean.getIp());
		Configuration.writeProperties(appPath.getPath()+propertyFile, "LOGIN_PORT", updateBean.getLogin_port());
		Configuration.writeProperties(appPath.getPath()+propertyFile, "SRU_PORT", updateBean.getSru_port());
		Configuration.writeProperties(appPath.getPath()+propertyFile, "SCU_SMU_PORT", updateBean.getScu_smu_port());
		Configuration.writeProperties(appPath.getPath()+propertyFile, "LocalhostIP", updateBean.getLocalhostIP());
		Configuration.writeProperties(appPath.getPath()+propertyFile, "LocalhostPort", updateBean.getLocalhostPort());
		Configuration.writeProperties(appPath.getPath()+propertyFile, "taskId", updateBean.getTaskid());
		Configuration.writeProperties(appPath.getPath()+propertyFile, "timer", updateBean.getTimer());
		
		boolean b=true;
		if(b){
			rsString="操作成功！";
			InitTaskService.init();
		}
		updateBean=null;
		Const.printScript(ServletActionContext.getResponse(),"<script>alert('"+rsString+"');window.location.href='task_index.action';</script>");
		return null;
		
	}

	public Pagenation getPage() {
		return page;
	}

	public void setPage(Pagenation page) {
		this.page = page;
	}

	public TaskBean getQueryBean() {
		return queryBean;
	}

	public void setQueryBean(TaskBean queryBean) {
		this.queryBean = queryBean;
	}

	public TaskBean getUpdateBean() {
		return updateBean;
	}

	public void setUpdateBean(TaskBean updateBean) {
		this.updateBean = updateBean;
	}

	public TaskBean getAddBean() {
		return addBean;
	}

	public void setAddBean(TaskBean addBean) {
		this.addBean = addBean;
	}
}
